# !/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :downloader.py
# @Time      :2025/7/11 23:43
# @Author    :aiyimu

import threading
import requests
import os
import time


def download_file(url, filename):
    print(f"开始下载 {filename}")
    response = requests.get(url, stream=True)
    with open(filename, 'wb') as f:
        for chunk in response.iter_content(chunk_size=8192):
            if chunk:
                f.write(chunk)
    print(f"完成下载 {filename}")


def main():
    urls = [
        ("https://example.com/file1.zip", "file1.zip"),
        ("https://example.com/file2.zip", "file2.zip"),
        ("https://example.com/file3.zip", "file3.zip"),
    ]

    threads = []
    for url, filename in urls:
        if os.path.exists(filename):
            print(f"{filename} 已存在，跳过下载")
            continue
        t = threading.Thread(target=download_file, args=(url, filename))
        threads.append(t)
        t.start()

    for t in threads:
        t.join()

    print("所有下载任务完成")


if __name__ == "__main__":
    start = time.time()
    main()
    print(f"总耗时: {time.time() - start:.2f}秒")